package top.dbhg.tasklist.hud;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.ColorArgumentType;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import top.dbhg.tasklist.Tasklist;

import java.util.Objects;

import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;

public class Command implements ModInitializer {

    private static final SuggestionProvider<ServerCommandSource> TASK_TAG_SUGGESTION_PROVIDER = (context, builder) ->
            CommandSource.suggestMatching(HUD.getTaskTags(), builder);
    public static void onInit(){
        CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
            dispatcher.register(literal("tasklist")
                    .then(literal("add")
                            .then(argument("tag", StringArgumentType.string())
                                    .then(argument("text",StringArgumentType.string())
                                            .then(argument("color", ColorArgumentType.color())
                                                    .executes(context -> {
                                                        String tag = getString(context, "tag");
                                                        String text = getString(context, "text");
                                                        if(Objects.equals(tag, "")){
                                                            context.getSource().sendFeedback(() -> Text.literal("参数不得为空"), false);
                                                            return 1;
                                                        }
                                                        Formatting color = ColorArgumentType.getColor(context, "color");
                                                        HUD.addTask(tag,text,color);
                                                        return 1;
                                            }))
                                    )
                            )
                    )
                    .then(literal("reset")
                            .executes(context -> {
                                HUD.reSet();
                                context.getSource().sendFeedback(() -> Text.literal("已经全部移除"), false);
                                return 1;
                            })
                    )
                    .then(literal("del")
                            .then(argument("tag",StringArgumentType.string())
                                    .suggests(TASK_TAG_SUGGESTION_PROVIDER)
                                    .executes(context -> {
                                        String value = getString(context, "tag");
                                        if(Objects.equals(value, "")){
                                            context.getSource().sendFeedback(() -> Text.literal("参数不得为空"), false);
                                            return 1;
                                        }
                                        HUD.completeTask(value);
                                        context.getSource().sendFeedback(() -> Text.literal(value+"已经移除"), false);
                                        return 1;
                                    })
                    ))
            );
        });
        Tasklist.Mod_DeBug("Command Loaded",null);
    }

    @Override
    public void onInitialize() {

    }
}
